package com.bsit.obu.utils;

import java.util.ArrayList;

public class ByteUtil {

	private final static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	/**
	 * bytes转换成十六进制字符串
	 */
	public static String byte2HexStr(byte[] a) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < a.length; n++) {
			stmp = (Integer.toHexString(a[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}

	/**
	 * 16进制字符串转换成int数组
	 *
	 * @param str
	 * @return
	 */
	public static byte[] hexStr(String str) {

		byte[] bt = new byte[str.length() / 2];
		for (int i = 0; i < str.length() / 2; i++) {
			bt[i] = (byte) Integer.parseInt(str.substring(i * 2, 2 * (i + 1)),
					16);
		}
		return bt;
	}


	public static int toInt(byte[] b, int s, int n) {
		int ret = 0;

		final int e = s + n;
		for (int i = s; i < e; ++i) {
			ret <<= 8;
			ret |= b[i] & 0xFF;
		}
		return ret;
	}

	public static int toIntR(byte[] b, int s, int n) {
		int ret = 0;

		for (int i = s; (i >= 0 && n > 0); --i, --n) {
			ret <<= 8;
			ret |= b[i] & 0xFF;
		}
		return ret;
	}

	public static String toHexString(byte[] d, int s, int n) {
		final char[] ret = new char[n * 2];
		final int e = s + n;

		int x = 0;
		for (int i = s; i < e; ++i) {
			final byte v = d[i];
			ret[x++] = HEX[0x0F & (v >> 4)];
			ret[x++] = HEX[0x0F & v];
		}
		return new String(ret);
	}

	public static String toAmountString(float value) {
		return String.format("%.2f", value);
	}

	/**
	 * 根据行为的代码，拼接指令
	 * @param sendOrReceiveFlag 发送or接受代码 高位为8：发送，低位为8：接收
	 * @param cmdType 操作类型代码 如： 为0x31 则为微博发射参数
	 * @param dataType 具体操作代码 如：cmdType = 0x31, dataType = 01,则为发送CW波
     * @return 可发送的指令
     */
	public static byte[] cmd2HexString(byte sendOrReceiveFlag, byte cmdType, byte dataType, byte param){
		byte flag;
		if (param == 0) {
			flag = (byte) (sendOrReceiveFlag ^ cmdType ^ dataType);
		} else {
			flag = (byte) (sendOrReceiveFlag ^ cmdType ^ dataType ^ param);
		}
//		MyLog.d("SerialPortAcitivity",cmdType + "flag is : " + flag);
		byte head1 = (byte) 0xFF;
		byte head2 = (byte) 0xFF;
		byte end = (byte) 0xFF;
		return new byte[]{head1,head2,sendOrReceiveFlag,cmdType,dataType,param,flag,end};
	}

	/**
	 * 命令转化为二进制数组
	 * @param data
	 * @return
     */
	public static byte[] cmd2HexByteArray(byte... data){
		byte flag = data[0];
		byte head1 = (byte) 0xFF;
		byte head2 = (byte) 0xFF;
		byte end = (byte) 0xFF;
		for (int i = 1; i < data.length; i++) {
			flag ^= data[i];
		}
		ArrayList<Byte> a = new ArrayList<Byte>();
		a.add(head1);
		a.add(head2);
		for (int i = 0; i < data.length; i++) {
			if (data[i] == (byte) 0xFF) {
				a.add((byte) 0xFE);
				a.add((byte) 0x01);
			} else if (data[i] == (byte) 0xFE) {
				a.add((byte) 0xFE);
				a.add((byte) 0x00);
			} else {
				a.add(data[i]);
			}
		}
		if (flag == (byte) 0xFF) {
			a.add((byte) 0xFE);
			a.add((byte) 0x01);
		} else if (flag == (byte) 0xFE) {
			a.add((byte) 0xFE);
			a.add((byte) 0x00);
		} else {
			a.add(flag);
		}
		a.add(end);
		byte[] result = new byte[a.size()];
		for(int i=0;i<a.size();i++){
			result[i]=a.get(i);

		}

		return result;
	}

	/**
	 * 将中文转化为asscii字节数组
	 * @param cnCmd
	 * @return
     */
	public static byte[] cnCmd2HexByteArray(String cnCmd){
		if  (cnCmd ==  null  ||  "" .equals(cnCmd)) {
			return   null ;
		}

		char [] chars = cnCmd.toCharArray();
		byte [] asciiArray =  new   byte [chars.length];

		for  ( int  i =  0 ; i < chars.length; i++) {
			asciiArray[i] = char2ASCII(chars[i]);
		}
		return  asciiArray;
	}

	/**
	 * 将字符转化为二进制数
	 * @param c
	 * @return
     */
	public   static   byte  char2ASCII( char  c) {
		return  ( byte ) c;
	}



	public static void main(String[] args) throws Exception {
		String a = "宁夏C4FE";
		byte[] carNo = a.getBytes("GB2312");
		//byte[] b = hexStr(a);
		System.out.println();
	}

}
